#include "oscillator.h"
#include "buffer.h"
#include "qkeymetro.h"
#include <cmath>

Oscillator::Oscillator() :
        freq_(440.),
        curPhase_(0.)
{
}

QSharedPointer <Buffer> & Oscillator::getNextFrame(QSharedPointer <Buffer> & buffer){

    const float PIx2 = 2. * 3.14159;
    QKeyMetro::SoundData * data = * buffer->getData();
    int size = buffer->getSize();
    float phaseStep = PIx2 * freq_/QKeyMetro::StaticData::sampleRate;
    for (int i =0; i<size; ++i){
        curPhase_ += phaseStep;
        while (curPhase_ >= PIx2) curPhase_-=PIx2;
        data[i] = sin(curPhase_);
    }
    return buffer;
}
